/*
 * Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于pps-cloud系统.
 */
package com.pitop.succedaneum.ctrl;

import com.alibaba.fastjson.JSON;
import com.pitop.core.entity.BeanRet;
import com.pitop.core.entity.Page;
import com.pitop.core.tools.redis.RedisUtils;
import com.pitop.succedaneum.entity.SuccedaneumMaterial;
import com.pitop.succedaneum.service.SuccedaneumMaterialSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

/**
 * 可替代料材 控制器
 *
 * @author berton
 */
@RestController
@RequestMapping("/succedaneumMaterial")
@Slf4j
@Api(value = "可替代料材控制器", description = "可替代料材控制器")
public class SuccedaneumMaterialCtrl {

    @Resource
    protected RedisUtils redisUtils;

    @Resource
    private SuccedaneumMaterialSV succedaneumMaterialSV;

    /**
     * 查询SuccedaneumMaterial一个详情信息
     *
     * @param id   id
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询SuccedaneumMaterial一个详情信息", notes = "查询SuccedaneumMaterial一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public SuccedaneumMaterial load(Long id, String code) {
        if (id == null) {
            return null;
        }
        if (code == null) {
            return null;
        }
        SuccedaneumMaterial succedaneumMaterial = succedaneumMaterialSV.load(id, code);
        log.info(JSON.toJSONString(succedaneumMaterial));
        return succedaneumMaterial;
    }


    /**
     * 根据条件id查询SuccedaneumMaterial一个详情信息
     *
     * @param id id
     * @return BeanRet
     */
    @ApiOperation(value = "查询SuccedaneumMaterial一个详情信息", notes = "查询SuccedaneumMaterial一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/load/id/{id}")
    @ResponseBody
    public SuccedaneumMaterial loadById(@PathVariable Long id) {
        if (id == null) {
            return null;
        }
        SuccedaneumMaterial succedaneumMaterial = succedaneumMaterialSV.loadById(id);
        log.info(JSON.toJSONString(succedaneumMaterial));
        return succedaneumMaterial;
    }

    /**
     * 根据条件code查询SuccedaneumMaterial一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询SuccedaneumMaterial一个详情信息", notes = "查询SuccedaneumMaterial一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/code/{code}")
    @ResponseBody
    public SuccedaneumMaterial loadByCode(@PathVariable String code) {
        if (code == null) {
            return null;
        }
        SuccedaneumMaterial succedaneumMaterial = succedaneumMaterialSV.loadByCode(code);
        log.info(JSON.toJSONString(succedaneumMaterial));
        return succedaneumMaterial;
    }


    /**
     * 查询SuccedaneumMaterial信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询SuccedaneumMaterial信息集合", notes = "查询SuccedaneumMaterial信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
    })
    @PostMapping(value = "/list")
    @ResponseBody
    public List<SuccedaneumMaterial> list(@RequestBody @ApiIgnore SuccedaneumMaterial succedaneumMaterial, Integer curPage, Integer pageSize) {
        Page<SuccedaneumMaterial> page = new Page<>(pageSize, curPage);
        List<SuccedaneumMaterial> succedaneumMaterials = succedaneumMaterialSV.list(succedaneumMaterial, page.genRowStart(), page.getPageSize());
        int total = succedaneumMaterialSV.count(succedaneumMaterial);
        page.setTotalRow(total);
        log.info(JSON.toJSONString(page));
        return succedaneumMaterials;
    }


    /**
     * 查询SuccedaneumMaterial信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询SuccedaneumMaterial信息集合", notes = "查询SuccedaneumMaterial信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "id", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @GetMapping(value = "/list/by")
    @ResponseBody
    public List<SuccedaneumMaterial> listByPk(Long id, String code, Integer curPage, Integer pageSize) {
        Page<SuccedaneumMaterial> page = new Page<>(pageSize, curPage);
        List<SuccedaneumMaterial> succedaneumMaterials = succedaneumMaterialSV.list(id, code, page.genRowStart(), page.getPageSize());
        int total = succedaneumMaterialSV.count(id, code);
        page.setTotalRow(total);
        log.info(JSON.toJSONString(page));
        return succedaneumMaterials;
    }

    /**
     * 统计SuccedaneumMaterial信息数量根据主键
     *
     * @return 总条数
     */
    @ApiOperation(value = " 统计SuccedaneumMaterial信息数量根据主键", notes = " 统计SuccedaneumMaterial信息数量根据主键")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @GetMapping(value = "/count/by")
    @ResponseBody
    public Integer count(Long id, String code) {
        return succedaneumMaterialSV.count(id, code);
    }

    /**
     * 统计SuccedaneumMaterial信息数量
     *
     * @return 总条数
     */
    @ApiOperation(value = "统计SuccedaneumMaterial信息数量", notes = "统计SuccedaneumMaterial信息数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "品牌名", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "描述", paramType = "query"),
            @ApiImplicitParam(name = "website", value = "官网", paramType = "query"),
            @ApiImplicitParam(name = "telephone", value = "联系电话", paramType = "query"),
            @ApiImplicitParam(name = "address", value = "地址", paramType = "query")
    })
    @PostMapping(value = "/count")
    @ResponseBody
    public Integer count(@RequestBody SuccedaneumMaterial succedaneumMaterial) {
        return succedaneumMaterialSV.count(succedaneumMaterial);
    }


    /**
     * 创建可替换材料
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建可替换材料", notes = "创建可替换材料")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "materialCode", value = "原料编码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "品牌名", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "描述", paramType = "query"),
            @ApiImplicitParam(name = "website", value = "官网", paramType = "query"),
            @ApiImplicitParam(name = "telephone", value = "联系电话", paramType = "query"),
            @ApiImplicitParam(name = "address", value = "地址", paramType = "query")
    })
    @PostMapping("/build")
    @ResponseBody
    public SuccedaneumMaterial build(@RequestBody @ApiIgnore SuccedaneumMaterial succedaneumMaterial) {
        succedaneumMaterialSV.save(succedaneumMaterial);
        return succedaneumMaterial;
    }


    /**
     * 修改可替换材料
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改可替换材料", notes = "修改可替换材料")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "materialCode", value = "原料编码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "品牌名", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "描述", paramType = "query"),
            @ApiImplicitParam(name = "website", value = "官网", paramType = "query"),
            @ApiImplicitParam(name = "telephone", value = "联系电话", paramType = "query"),
            @ApiImplicitParam(name = "address", value = "地址", paramType = "query")
    })
    @PutMapping("/modify")
    @ResponseBody
    public SuccedaneumMaterial modify(@RequestBody @ApiIgnore SuccedaneumMaterial succedaneumMaterial) {
        succedaneumMaterialSV.modify(succedaneumMaterial);
        return succedaneumMaterial;
    }

    /**
     * 删除SuccedaneumMaterial
     *
     * @return BeanRet
     */
    @ApiOperation(value = "删除SuccedaneumMaterial", notes = "删除SuccedaneumMaterial")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @DeleteMapping("/delete")
    @ResponseBody
    public BeanRet delete(Long id, String code) {
        succedaneumMaterialSV.delete(id, code);
        return BeanRet.create(true, "删除SuccedaneumMaterial成功");
    }

}
